package com.example.hospitalsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalsystem.mapper.CaseMapper;
import com.example.hospitalsystem.mapper.PrescriptionMapper;
import com.example.hospitalsystem.mapper.RegistrationMapper;
import com.example.hospitalsystem.pojo.dto.caseP.AddCaseInfo;
import com.example.hospitalsystem.pojo.dto.caseP.CasePageQueryDto;
import com.example.hospitalsystem.pojo.dto.caseP.DoctorChangeCaseDto;
import com.example.hospitalsystem.pojo.entity.Case;
import com.example.hospitalsystem.pojo.entity.Registration;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.service.CaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CaseServiceImpl extends ServiceImpl<CaseMapper, Case> implements CaseService {

    @Autowired
    private CaseMapper caseMapper;

    @Autowired
    private PrescriptionMapper prescriptionMapper;

    @Autowired
    private RegistrationMapper registrationMapper;

    @Override
    public Result<Object> adminGetAllCase(CasePageQueryDto casePageQueryDto) {
        LambdaQueryWrapper<Case> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        Page<Case> casePage=new Page<>(casePageQueryDto.getPageNum(),casePageQueryDto.getPageSize());

        if(casePageQueryDto.getPatientName()!=null&& casePageQueryDto.getPatientName()!=""){
            lambdaQueryWrapper.eq(Case::getPatientName,casePageQueryDto.getPatientName());
            IPage<Case> caseList=caseMapper.selectPage(casePage,lambdaQueryWrapper);
            return Result.build(caseList,200,"返回所有病例信息成功");
        }

        //获取所有病人信息
        IPage<Case> caseList=caseMapper.selectPage(casePage,null);

        return Result.build(caseList,200,"返回所有病例信息成功");

    }

    @Override
    public Result<Object> doctorGetAllCase(CasePageQueryDto casePageQueryDto) {
        LambdaQueryWrapper<Case> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        Page<Case> casePage=new Page<>(casePageQueryDto.getPageNum(),casePageQueryDto.getPageSize());

        if(casePageQueryDto.getPatientName()!=null&& casePageQueryDto.getPatientName()!=""){
            lambdaQueryWrapper.eq(Case::getPatientName,casePageQueryDto.getPatientName());
            IPage<Case> caseList=caseMapper.selectPage(casePage,lambdaQueryWrapper);
            return Result.build(caseList,200,"返回所有病例信息成功");
        }

        //获取所有病人信息
        IPage<Case> caseList=caseMapper.selectPage(casePage,null);

        return Result.build(caseList,200,"返回所有病例信息成功");
    }


    @Override
    public Result<Object> adminDeleteCase(int id) {
        LambdaQueryWrapper<Case> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Case::getCaseId,id);
        int i=caseMapper.delete(lambdaQueryWrapper);
        if(i>0){
            return Result.build("管理员删除病例信息成功");
        }
        return Result.build("管理员删除病例信息失败");
    }

}
